#include <os/mutex.h>
#include <freertos/include/FreeRTOS.h>
#include <freertos/include/semphr.h>


/**
 * @brief initialize a mutex
 * @param mutex lock
 * @return none
 */
void mutex_init(struct mutex *mutex) 
{
    mutex->mutex_ptr = xSemaphoreCreateMutex();
}


/**
 * @brief lock a mutex
 * @param mutex lock
 * @return none
 */
void mutex_lock(struct mutex *mutex)
{
    xSemaphoreTake((SemaphoreHandle_t)mutex->mutex_ptr, portMAX_DELAY);
}


/**
 * @brief unlock a mutex
 * @param mutex lock
 * @return none
 */
void mutex_unlock(struct mutex *mutex)
{
    xSemaphoreGive((SemaphoreHandle_t)mutex->mutex_ptr);
}


/**
 * @brief destroy a mutex
 * @param mutex lock
 * @return none
 */
void mutex_destroy(struct mutex *mutex)
{
    vSemaphoreDelete((SemaphoreHandle_t)mutex->mutex_ptr);
}
